package com.xebest.web.controller.aftersales;

import com.github.pagehelper.PageHelper;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.util.DateUtil;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.msg.XeVenAfterSaleMsg;
import com.xebest.web.form.XeVenOrderCreditcardRecordsForm;
import com.xebest.web.model.base.XePageInfo;
import com.xebest.web.service.XeVenOrderCreditcardRecordsService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.XeVenOrderCreditCardRecordsView;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjing
 */
@RestController
@RequestMapping("creditcardRecords")
@Scope("prototype")
public class XeVenOrderCreditcardRecordsController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(XeVenOrderCreditcardRecordsController.class);//日志记录类

    @Autowired
    private XeVenOrderCreditcardRecordsService xeVenOrderCreditcardRecordsService;


    @RequestMapping(value = "queryCreditcardRecordsList", method = RequestMethod.POST)
    public
    @ResponseBody
    BaseResponse queryCreditcardRecordsList(@RequestBody XeVenOrderCreditcardRecordsForm form) {
        log.info("查询POS刷卡记录:{}" , ToStringBuilder.reflectionToString(form));
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        if (null == form) {
            form = new XeVenOrderCreditcardRecordsForm();
        }
        form.setUserCode(userInfo.getUserCode());
        String createStartDate = form.getOrderStartDate();
        String createEndDate = form.getOrderEndDate();
        //设定查询中日期为3个月以内
        if(StringUtils.isNotBlank(createStartDate) && StringUtils.isNotBlank(createEndDate)){
            Boolean dateCm = DateUtil.comparableTime(createStartDate,createEndDate);
            if(!dateCm){
                return sendAjaxError(XeVenAfterSaleMsg.AFTER_SALE_DATE_COMPARABLE);
            }
        }
        if (StringUtils.isBlank(createStartDate) && StringUtils.isBlank(createEndDate)){
            createEndDate = DateUtil.getDate(new Date());;
            createStartDate = DateUtil.strToNMonth(createEndDate,-3);
        }
        if(StringUtils.isBlank(createStartDate) && StringUtils.isNotBlank(createEndDate)){
            form.setOrderStartDate(DateUtil.strToNMonth(createEndDate,-3));
        }
        if(StringUtils.isBlank(createEndDate) && StringUtils.isNotBlank(createStartDate)){
            form.setOrderEndDate(DateUtil.strToNMonth(createStartDate,3));
        }
        if (StringUtils.isNotBlank(form.getFormIdType())) {
            int formIdType = Integer.parseInt(form.getFormIdType());
            switch (formIdType) {
                case 1:   //订单编号
                    form.setOrderId(form.getFormIdValue());
                    break;
                case 2:   //支付流水号,
                    form.setPayNumber(form.getFormIdValue());
                    break;
                case 3:   //交易终端号
                    form.setTerminals(form.getFormIdValue());
                    break;
                default:
                    break;
            }
        }
        XePageInfo<XeVenOrderCreditCardRecordsView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        try {
            //售后单列表
            List<XeVenOrderCreditCardRecordsView> creditCardRecordsViewList = xeVenOrderCreditcardRecordsService.queryCreditCardRecordsList(form);
            xePageInfo = new XePageInfo<>(creditCardRecordsViewList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("creditCardRecordsViewList", creditCardRecordsViewList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("================查询pos刷卡记录异常 ================,{}", e);
            return sendAjaxError();
        }
    }


}
